package com.qf.dao;

import com.qf.entity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao{
   @Override
    public User login(String username, String password) {

        //jdbc数据库操作
       Connection conn = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       User user = null;
       try {
           Class.forName("com.mysql.cj.jdbc.Driver");
           conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2503?useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true", "root", "050206yrx");
           String sql = "select * from tb_user where username = ? and password = ?";
           ps = conn.prepareStatement(sql);
           ps.setString(1,username);
           ps.setString(2,password);

           rs = ps.executeQuery( );
           if (rs.next()){
               user = new User(  );
               user.setId(rs.getInt("id"));
               user.setUsername(rs.getString("username"));
               user.setPassword(rs.getString("password"));
               user.setPhone(rs.getString("phone"));
               user.setCreateTime(rs.getDate("create_time"));
               user.setMoney(rs.getDouble("money"));
               user.setSex(rs.getInt("sex"));
           }
       }catch (Exception e) {
           e.printStackTrace();
       }finally {
           try {
               rs.close( );
               ps.close( );
               conn.close( );
           }catch (Exception e) {
               e.printStackTrace();
           }
       }
       return user;
    }
}
